using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Rotempco.Core.Web.Logic
{
    public class WebFormReadOnly
    {
        public static void MakeReadOnly(Control container)
        {
            for (int i = container.Controls.Count - 1; i >= 0; i--)
            {
                Control control = container.Controls[i];
                MakeReadOnly(container, control);
            }
        }

        private static void MakeReadOnly(Control container, Control control)
        {
            if (control is TextBox)
            {
                TextBox textBox = (control as TextBox);
                Literal literal = new Literal()
                {
                    Text = textBox.Text,
                    ID = "literal_" + textBox.ID.Substring(textBox.ID.IndexOf('_') + 1)
                };

                int index = container.Controls.IndexOf(textBox);
                container.Controls.Remove(textBox);
                container.Controls.AddAt(index, literal);
            }

            //if (control is CalendarExtender)
            //{
            //    CalendarExtender calendarExtender = (control as CalendarExtender);
            //    container.Controls.Remove(calendarExtender);
            //}

            foreach (Control innerControl in control.Controls)
            {
                MakeReadOnly(control, innerControl);
            }
        }
    }
}
